package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.wan37.client.handler.ClientHandler;
import com.wan37.client.service.cache.ChatCache;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameChat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author : luoyong
 * @date : 2020-07-15 11:13
 **/
@Component(PtoNum.CHATRESPONSE + "")
public class ChatResponse implements ReceiveMsg {
    private final Logger logger = LoggerFactory.getLogger(ChatResponse.class);
    private final ClientHandler clientHandler = ClientHandler.getInstance();
    @Autowired
    private ChatCache chatCache;

    @Override
    public void execute(Message msg) {
        GameChat.ChatResponse chatResponse = (GameChat.ChatResponse) msg;
        int code = chatResponse.getCode();
        String userId2 = null;
        if (!chatResponse.getChatList().isEmpty()) {
            userId2 = chatResponse.getChat(0).getSenderId();
        }
        switch (code) {
            case PtoNum.ONETOONECHAT:
                if (!chatCache.getUserIdToChatIn().get(clientHandler.selfId)
                        .getOneChatListMap().containsKey(userId2)) {
                    chatCache.getUserIdToChatIn().get(clientHandler.selfId)
                            .getOneChatListMap().put(userId2, new ArrayList<>());
                }
                List<GameChat.Chat> oneChatList = chatCache.getUserIdToChatIn()
                        .get(clientHandler.selfId).getOneChatListMap().get(userId2);
                oneChatList.add(chatResponse.getChat(0));
                logger.info("{} 说：{}", chatResponse.getChat(0).getSenderId(), chatResponse.getChat(0).getContent());
                break;
            case PtoNum.WORLDPLAYERCHAT:
                chatCache.getWorldChatList().add(chatResponse.getChat(0));
                printfChat(chatResponse.getChat(0));
                break;
            case PtoNum.ONETOONECHATOLD:
                if (userId2 == null) {
                    return;
                }
                if (!chatCache.getUserIdToChatIn().get(clientHandler.selfId)
                        .getOneChatListMap().containsKey(userId2)) {
                    chatCache.getUserIdToChatIn().get(clientHandler.selfId)
                            .getOneChatListMap().put(userId2, new ArrayList<>());
                }
                List<GameChat.Chat> oneChatList1 = chatCache.getUserIdToChatIn()
                        .get(clientHandler.selfId).getOneChatListMap().get(userId2);
                oneChatList1.clear();
                for (GameChat.Chat chat : chatResponse.getChatList()) {
                    oneChatList1.add(chat);
                    printfChat(chat);
                }
                break;
            case PtoNum.WORLDPLAYERCHATOLD:
                for (GameChat.Chat chat : chatResponse.getChatList()) {
                    chatCache.getWorldChatList().add(chat);
                    printfChat(chat);
                }
            default:
                break;
        }
    }

    private void printfChat(GameChat.Chat chat) {
        if (chat.getSenderId().equals(clientHandler.selfId)) {
            logger.info("我说：{}", chat.getContent());
        } else {
            logger.info("{} 说：{}", chat.getSenderId(), chat.getContent());
        }
    }
}
